package com.hsc.eduservice.service.impl;

import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hsc.eduservice.entity.EduSubject;
import com.hsc.eduservice.entity.excel.SubjectData;
import com.hsc.eduservice.entity.subject.OneSubject;
import com.hsc.eduservice.entity.subject.TwoSubject;
import com.hsc.eduservice.listener.SubjectExcelListener;
import com.hsc.eduservice.mapper.EduSubjectMapper;
import com.hsc.eduservice.service.EduSubjectService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 课程科目 服务实现类
 * </p>
 *
 * @author testjava
 * @since 2022-10-13
 */
@Service
public class EduSubjectServiceImpl extends ServiceImpl<EduSubjectMapper, EduSubject> implements EduSubjectService {

    @Override
    public void saveSubject(MultipartFile file,EduSubjectService eduSubjectService) {

        try {
            InputStream in = file.getInputStream();
            EasyExcel.read(in, SubjectData.class,new SubjectExcelListener(eduSubjectService)).sheet().doRead();
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    @Override
    public List<OneSubject> getAllOneTwoSubjecdt() {

        //查询所有一级分类 pid=0
        QueryWrapper<EduSubject> wrapperone = new QueryWrapper<>();
        wrapperone.eq("parent_id",0);
        List<EduSubject> oneSubjectList = baseMapper.selectList(wrapperone);

        //查询所有二级分类 pid！=0
        QueryWrapper<EduSubject> wrappertwo = new QueryWrapper<>();
        wrappertwo.ne("parent_id",0);
        List<EduSubject> twoSubjectList = baseMapper.selectList(wrappertwo);
//        System.out.println(twoSubjectList.size()); 调测试使用

        //创建list集合，用于存储最终封装数据
        List<OneSubject> finalSubject = new ArrayList<>();

        //封装一级分类
        for (int i = 0; i < oneSubjectList.size(); i++) {
            EduSubject eduSubject = oneSubjectList.get(i);
            OneSubject oneSubject = new OneSubject();
//            oneSubject.setId(eduSubject.getId());
//            oneSubject.setTitle(eduSubject.getTitle());
            //数据转换快捷操作
            BeanUtils.copyProperties(eduSubject,oneSubject);

            //数据存储
            //封装二级分类
            //这段代码时间复杂度太高，可优化
            finalSubject.add(oneSubject);
            List<TwoSubject> finalTwoSubject = new ArrayList<>();
            for (int n = 0; n < twoSubjectList.size(); n++) {
                EduSubject tSubject = twoSubjectList.get(n);
                if(tSubject.getParentId().equals(eduSubject.getId())){
                    TwoSubject twoSubject = new TwoSubject();
                    BeanUtils.copyProperties(tSubject,twoSubject);
                    finalTwoSubject.add(twoSubject);
                }
            }
//            System.out.println(finalTwoSubject.size()); 测试使用
            oneSubject.setChildren(finalTwoSubject);
        }

        return finalSubject;
    }
}
